/*
 * Copyright (c) 2009, Jan Pastor. All Rights Reserved.
 */

package sk.jp.jitag.dao.db4o.model;

import com.db4o.ObjectContainer;
import com.db4o.query.Predicate;

import sk.jp.jitag.dao.db4o.Column;
import sk.jp.jitag.dao.db4o.CascadeOnDelete;
import sk.jp.jitag.dao.db4o.BaseEntity;

public class UserProfile extends BaseEntity {

    private static final Column _ADDRESS = new Column("address");

    private String address;
    private String phoneNumber = null;
    private UserNote note = null;

    //----------------------------------------------------------------------

    public UserProfile(String address) {
        checkNotNullNotEmpty(_ADDRESS, address);
        this.address = address;
    }

    public String getAddress() {
        return address;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public UserProfile setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
        return this;
    }

    public UserNote getNote() {
        return note;
    }

    public UserProfile setNote(UserNote note) {
        this.note = note;
        return this;
    }

    /**
     * Check constraints before delete, because {@link User#profile} is declared
     * with {@link CascadeOnDelete} annotation.
     * @param db
     * @return true only if doesn't exists {@link User} that references this
     * instance.
     */
    public boolean objectCanDelete(ObjectContainer db){
        return db.query(new Predicate<User>() {
                        public boolean match(User user) {
                            return user.getProfile().equals(UserProfile.this);
                        }
                    }).isEmpty();
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("UserProfile");
        sb.append("{address='").append(address).append('\'');
        sb.append(", phoneNumber='").append(phoneNumber).append('\'');
        sb.append(", note=").append(note);
        sb.append('}');
        return sb.toString();
    }
}
